package com.ganbing518.commons.api.concurrent.support;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CountDownLatch;

/**
 * Description:
 *
 * @author gan bing
 * @version V1.0
 * @date 2018-06-27 9:35
 */
public class ConcurrentRunnable implements Runnable {

    private final static Logger LOGGER = LoggerFactory.getLogger(ConcurrentRunnable.class);

    private CountDownLatch begin;
    private CountDownLatch end;
    private Task task;
    private int count;

    public ConcurrentRunnable(CountDownLatch begin, CountDownLatch end, Task task, int count) {
        this.begin = begin;
        this.end = end;
        this.task = task;
        this.count = count;
    }

    @Override
    public void run() {
        try {
            begin.await();
            LOGGER.info("ConcurrentRunnable run task " + count + " start");
            task.doTask();
            LOGGER.info("ConcurrentRunnable run task " + count + " end");
        } catch (InterruptedException e) {
            LOGGER.error("ConcurrentRunnable run Exception", e);
        } finally {
            end.countDown();
        }
    }
}
